package com.liws.hellojuc.CyclicBarrier;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CyclicBarrier;

/**
 * @author ：liweisheng
 * @date ：2022/4/3 8:57
 * @description：
 */
public class CyclicBarrierTest {

    static class TaskThread extends Thread {

        CyclicBarrier barrier;

        public TaskThread(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void run() {
            try {

                DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy MM dd hh:mm:SS k");

                LocalDateTime arrivalDateA1 = LocalDateTime.now();
                String landingA1 = arrivalDateA1.format(format);
                System.out.println(getName() + " 到达栅栏 A，时间："+landingA1);
                barrier.await();
                LocalDateTime arrivalDateA2 = LocalDateTime.now();
                String landingA2 = arrivalDateA2.format(format);
                System.out.println(getName() + " 冲破栅栏 A，时间："+landingA2);

/*                LocalDateTime arrivalDateB1 = LocalDateTime.now();
                String landingB1 = arrivalDateB1.format(format);
                System.out.println(getName() + " 到达栅栏 B，时间："+landingB1);
                barrier.await();
                LocalDateTime arrivalDateB2 = LocalDateTime.now();
                String landingB2 = arrivalDateB2.format(format);
                System.out.println(getName() + " 冲破栅栏 B，时间："+landingB2);*/
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        int threadNum = 5;
        CyclicBarrier barrier = new CyclicBarrier(threadNum, new Runnable() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 完成最后任务");
            }
        });

        for(int i = 0; i < threadNum; i++) {
            new TaskThread(barrier).start();
        }
        System.out.println("111");
    }



}
